# tile.auk
# Tim Menzies
# Sept 12, 2012

"""

stuff 22  3  

"""

@include "lib.awk"

function tile(lst,chops,width,form, lo,hi,   \
              bar,sorted,p,who,where,wheres0,	\
              wheres,i,j,n,w,out,start,stop,\
              median,spread,max) {
  lo   =    lo == "" ? 0        : lo
  hi   =    hi == "" ? 100      : hi
  form =  form == "" ? " %3.0f" : form
  width= width == "" ? 50       : width
  bar   ="|"   
  for(i=1;i<=width;i++) 
    out[i] = " "
  n=asort(lst,sorted)
  for(p in chops) {  
    who[p] = sorted[int(p*n) ]
    where  = int(width*(who[p] - lo)/(hi-lo))
    wheres0[p]["x"] = where       
    wheres0[p]["*"] = chops[p]
  }
  w=asort(wheres0,wheres,"xsort")
  for(i=1;i<=w;i++) {
    start = wheres[i]["x"]
    stop  = i==w ? width : wheres[i+1]["x"]
    for(j=start;j<=stop;j++)
	    out[j] = wheres[i]["*"]
  }
  out[int(width/2)] = bar
  median  = sorted[int(0.5*n)]
  spread  = sorted[int(0.7*n)] - sorted[int(0.3*n)]
  max     = sorted[int(0.9*n)]
  where      = int(width*(median - lo)/(hi - lo))
  out[where] = "*" 
  asort(who)
  return l2s(who,", ",form)  \
    sprintf(",|," form ",",           \
            spread)  ",>,"l2s(out,"") ",<, "
}
 
#eg
function _tile(      i,lst,chops1,chops2) {
  for(i=1;i<=1000;i++) 
    lst[i] = rand()^2
  pairs("0.1,-,0.3, ,0.5, ,0.7,-,0.9, ",chops1)
  pairs("0.25,-,0.5,-,0.75, "          ,chops2)
  print tile(lst,chops1,50,"%3.2f",0,1)
  print tile(lst,chops2,40,"%3.2f",0,1)
  print tile(lst,chops2,30,"%3.2f",0,1)
}
